package com.bins.dentist.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bins.dentist.clinic.constants.CommonConstants;
import com.bins.dentist.clinic.mapper.DeptMapper;
import com.bins.dentist.clinic.model.dto.DeptDto;
import com.bins.dentist.clinic.model.dto.DeptPageDto;
import com.bins.dentist.clinic.entity.DeptEntity;
import com.bins.dentist.clinic.model.vo.DeptVo;
import com.bins.dentist.clinic.service.DeptService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bins
 * @since 2023-02-20 20:53:30
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, DeptEntity> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public IPage<DeptVo> getPage(DeptPageDto pageDto) {
        LambdaQueryWrapper<DeptEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(!ObjectUtils.isEmpty(pageDto.getDeptCode()), DeptEntity::getDeptCode, pageDto.getDeptCode())
                .like(!ObjectUtils.isEmpty(pageDto.getDeptName()), DeptEntity::getDeptName, pageDto.getDeptName())
                .eq(DeptEntity::getIsDel, CommonConstants.DELETED_NO);
        IPage page = deptMapper.selectPage(new Page(pageDto.getPageNum(), pageDto.getPageSize()), queryWrapper);
        List<DeptEntity> deptPageList = page.getRecords();
        if (CollectionUtils.isEmpty(deptPageList)) {
            return page;
        }
        List<DeptVo> deptList = Lists.newArrayListWithCapacity(deptPageList.size());
        for (DeptEntity deptEntity : deptPageList) {
            DeptVo roleVo = new DeptVo();
            BeanUtils.copyProperties(deptEntity, roleVo);
            deptList.add(roleVo);
        }
        page.setRecords(deptList);
        return page;
    }

    @Override
    public DeptVo getDetailById(DeptDto dto) {
        DeptEntity deptEntity = deptMapper.selectById(dto.getId());
        DeptVo vo = new DeptVo();
        BeanUtils.copyProperties(deptEntity, vo);
        return vo;
    }

    @Override
    public Integer deleteDept(DeptDto dto) {
        DeptEntity entity = new DeptEntity();
        entity.setId(dto.getId());
        entity.setIsDel(CommonConstants.DELETED_YES);
        return deptMapper.updateById(entity);
    }

    @Override
    public DeptVo saveDept(DeptDto dto) {
        DeptEntity entity = new DeptEntity();
        if (ObjectUtils.isEmpty(dto.getId())) {
            BeanUtils.copyProperties(dto, entity);
            entity.setPid(ObjectUtils.isEmpty(dto.getPid()) ? 0 : dto.getPid());
            entity.setSort(dto.getSort());
            entity.setIsDel(CommonConstants.DELETED_NO);
            entity.setCreateBy(0L);
            entity.setUpdateBy(0L);
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            deptMapper.insert(entity);
        } else {
            entity.setId(dto.getId());
            entity.setDeptCode(dto.getDeptCode());
            entity.setDeptName(dto.getDeptName());
            entity.setPid(ObjectUtils.isEmpty(dto.getPid()) ? 0 : dto.getPid());
            entity.setSort(dto.getSort());
            entity.setUpdateBy(999999L);
            entity.setUpdateTime(LocalDateTime.now());
            deptMapper.updateById(entity);
        }
        DeptVo vo = new DeptVo();
        vo.setId(entity.getId());
        vo.setDeptCode(entity.getDeptCode());
        vo.setDeptName(entity.getDeptName());
        return vo;
    }
}
